package com.iheima.service;

import com.iheima.dao.UserDao;
import com.iheima.domain.PageBean;
import com.iheima.domain.User;

import java.util.List;

public class UserService {
    private UserDao userDao =new UserDao();

    public List<User>queryAll(){
        return userDao.queryAll();
    }

    public boolean chekname(String name) {
        User user = userDao.chekname(name);
        return user!=null;
    }

    public void add(User user) {
        userDao.add(user);
    }

    public User findById(int id) {
        return userDao.findById(id);
    }

    public void edit(User user) {
        userDao.edit(user);
    }

    public void delete(int id) {
        userDao.delete(id);
    }

    public PageBean<User> pageQuery(int pageSize, int pageNumber) {
        PageBean<User> pageBean = new PageBean<>();
        pageBean.setPageNumber(pageNumber);
        pageBean.setPageSize(pageSize);

        int totalCount=userDao.totalCount();
        pageBean.setTotalCount(totalCount);

        int pageCount=(int)Math.ceil(totalCount*1.0/pageSize);
        pageBean.setPageCount(pageCount);

        int startIndex = (pageNumber-1)*pageSize;
        List<User>userList=userDao.pageQuery(startIndex, pageSize);
        pageBean.setDataList(userList);

        return pageBean;
    }
}
